import { defineStore } from "pinia"
import { ref } from "vue"
import {LoginApi} from "@/apis/login"
import {mergeCart} from "@/apis/cart"
import { useCartStore } from "./cart"
export const  useUserStore=defineStore('rabbit-user',()=>{
    const userInfo=ref({})
    const cartStore=useCartStore()
    cartStore.getCart()
    const getUserInfo=async({account,password})=>{
        //获取用户信息
        console.log('获取用户信息')
       const res=await LoginApi({account,password})
       userInfo.value=res.result
       console.log('获取成功')
       console.log('ces',cartStore.cartList)
       const addlist=cartStore.cartList.map(item=>{
        return{
            skuId:item.skuId,
            selected:item.selected,
            count:item.count
            
        }
       })
       console.log('addlist',addlist)
       await mergeCart(addlist)
       console.log('合并成功')
       cartStore.updateNewList()
       

    }
    const ClearUserInfo=()=>{
        //清除用户信息
        userInfo.value={}
    }
    return{
        userInfo,
        getUserInfo,
        ClearUserInfo
    }
},{
    persist: true
})